package com.turing.post.common.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.BlackList;
import com.turing.post.common.util.BlackListDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 黑名单
 * 
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "common.impl.BlackListDaoImpl")
@Remote
@Local
public class BlackListDaoImpl implements BlackListDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public BlackList getBlackListByBlackList(BlackList blackList) {
		if (blackList == null || blackList.getIdTypeId() == null
				|| PCPSUtil.isEmpty(blackList.getIdNo())) {
			return null;
		} else {
			return _em.find(BlackList.class, blackList);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BlackList> getBlackListByIdNo(String idNo) {
		String jpql = "select * from BlackList c where c. idNo like ?1 order by idTypeId,idNo";
		Query query = _em.createNativeQuery(jpql, BlackList.class);
		query.setParameter(1, "%" + idNo + "%");
		List<BlackList> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BlackList> getBlackListByName(String name) {
		String jpql = "select * from BlackList c where c.name like ?1 order by idTypeId,idNo";
		Query query = _em.createNativeQuery(jpql, BlackList.class);
		query.setParameter(1, "%" + name + "%");
		List<BlackList> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BlackList> getBlackListList(Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(
				"select * from BlackList t order by IdTypeId", BlackList.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getBlackListTotal() {
		Query query = _em
				.createNativeQuery("select nvl(count(*),0) count from BlackList t");
		return ((BigDecimal) query.getSingleResult()).toBigInteger();
	}

	@Override
	public void insertBlackList(BlackList blackList)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, blackList);
	}

	@Override
	public void updateBlackList(BlackList blackList)
			throws PCPSApplicationException {
		GeneralDao.update(_em, blackList);
	}

	@Override
	public void deleteBlackList(BlackList blackList)
			throws PCPSApplicationException {
		GeneralDao.remove(_em, getBlackListByBlackList(blackList));
	}
}
